//
//  ViewController.m
//  PuBuLiu
//
//  Created by @Zs on 15/12/25.
//  Copyright © 2015年 @Zs. All rights reserved.
//

#import "ViewController.h"
#import "ZSWaterFlowLayout.h"
#import "ZSShopCell.h"
#import "ZSShop.h"
#import "MJExtension.h"

@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,ZSWaterFlowLayoutDelegate>
@property (weak,nonatomic) UICollectionView *collectionView;
@property (nonatomic , strong) NSArray *shops;
@end

@implementation ViewController

static NSString * const CellId  = @"shop";

-(BOOL)prefersStatusBarHidden{
    
    return YES;
}



- (void)viewDidLoad {
    [super viewDidLoad];
   
    //初始化数据
    self.shops = [ZSShop objectArrayWithFilename:@"1.plist"];
    //创建布局
    ZSWaterFlowLayout  *layout = [[ZSWaterFlowLayout alloc]init];
    layout.delegate = self;
    //创建collectionView
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
    
    collectionView.dataSource = self;
    collectionView.delegate = self;
    [collectionView registerNib:[UINib nibWithNibName:@"ZSShopCell" bundle:nil] forCellWithReuseIdentifier:CellId];
    
    [self.view addSubview:collectionView];
}


#pragma mark -<ZSWaterFlowLayoutDelegate>
-(CGFloat)waterflowLayout:(ZSWaterFlowLayout *)waterflowLayout heightForItemAtIndexPath:(NSIndexPath *)indexPath itemWidth:(CGFloat)itemWidth{
    ZSShop *shop = self.shops[indexPath.item];
    return shop.h * itemWidth / shop.w;

//    return arc4random_uniform(100) + 100;
}

#pragma mark -<UICollectionViewDataSource>
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ZSShopCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellId forIndexPath:indexPath];
    cell.shop = self.shops[indexPath.item];
    return cell;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return self.shops.count;
}

#pragma mark -<UICollectionViewDelegate>
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    
}
@end
